<?php
/*
 +----------------------------------------------------------------------
 + Title        : Factory 控制器
 + Author       : Randy_chen
 + Version      : V1.0.0.1
 + Initial-Time : 2019/5/30 20:41
 + Last-time    : 2019/5/30 20:41+ 86187
 + Desc         : Factory
 +----------------------------------------------------------------------
*/


namespace app\common\lib;

use coupon\marketing\Coupon;
use crm\sap\SapCrm;
use crm\tongcheng\TongChengCrm;
use ReflectionClass;
use ReflectionException;

class Factory
{
    const SMS_NAMESPACE = 'sms\sms\\';
    const CRM = 'tongcheng\\TongChengCrm';
    const CRM_NAMESPACE = 'crm\\';
    const COUPON_NAMESPACE = 'coupon\\';

    public static function createSms()
    {
        //获取短信平台名称
        $sms = sysconf('sms_config|raw');
        $info = json_decode($sms, true);

        if (!$info) {
            throw new \InvalidArgumentException('短信配置解析失败!!!');
        }
        if (!isset($info['name'])) {
            throw new \InvalidArgumentException('短信供应商未正确配置!!!');
        }
        $className = self::SMS_NAMESPACE . $info['name'];
        try {
            $class = new ReflectionClass($className);
            $instance = $class->newInstance();
        } catch (ReflectionException $Exception) {
            throw new \InvalidArgumentException('暂不支持的短信发送类型');
        }
        return $instance;
    }


    public static function createCrm()
    {
        return new SapCrm();
        $className =  self::CRM_NAMESPACE.self::CRM;
        try {
            $class = new ReflectionClass($className);
            $instance = $class->newInstance();
        } catch (ReflectionException $Exception) {
            throw new \InvalidArgumentException('实例化失败!');
        }
        return $instance;
    }

    //获取前端代码模板类
    public static function createTmp($class)
    {
        $className = $class;
        try {
            $class = new ReflectionClass($className);
            $instance = $class->newInstance();
        } catch (ReflectionException $Exception) {
            throw new \InvalidArgumentException('实例化失败!');
        }
        return $instance;
    }

    /**
     * 获取优惠券的处理类
     *
     * @param  string $coupon_type 优惠券类型 与券的命名空间对应
     *
     * @return object
     * @author : Randy_chen
     * @Date   : 2019/7/16
     * @Time   : 15:01
     */
    public static function createCouponService($coupon_type){
        return new Coupon();
        $className = self::COUPON_NAMESPACE.$coupon_type.'\Coupon';
        try {
            $class = new ReflectionClass($className);
            $instance = $class->newInstance();
        } catch (ReflectionException $Exception) {
            throw new \InvalidArgumentException('实例化失败!');
        }
        return $instance;
    }
}